﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace DSSDataServiceGenerator.Controls
{
    public class ModelDesignerCanvasPanel
        : Canvas
    {
        public ModelDesignerCanvasPanel()
            : base()
        { }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            Size result = base.ArrangeOverride(arrangeSize);

            if (this.Children.Count > 0)
            {
                this.Width = this.Children.OfType<UIElement>().Select(item => Canvas.GetLeft(item)).Max() + 500;

                this.Height = this.Children.OfType<UIElement>().Select(item => Canvas.GetTop(item)).Max() + 500;
            }

            return result;
        }

        protected override Size MeasureOverride(Size constraint)
        {
            return base.MeasureOverride(constraint);
        }

        //public ItemsPanelTemplate ItemsPanel
        //{
        //    get { return (ItemsPanelTemplate)GetValue(ItemsPanelProperty); }
        //    set { SetValue(ItemsPanelProperty, value); }
        //}

        //public static readonly DependencyProperty ItemsPanelProperty = DependencyProperty.Register("ItemsPanel", typeof(ItemsPanelTemplate), typeof(ModelDesignerCanvasPanel),
        //    new FrameworkPropertyMetadata(new ItemsPanelTemplate(new FrameworkElementFactory(typeof(StackPanel))),
        //        FrameworkPropertyMetadataOptions.AffectsMeasure |
        //        FrameworkPropertyMetadataOptions.AffectsArrange,
        //        ItemsPanelProperty_Changed));

        //private static void ItemsPanelProperty_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        //{
        //    (sender as ModelDesignerCanvasPanel).LoadPanel();
        //}

        //public Panel InternalPanel { get; private set; }

        //private void LoadPanel()
        //{
        //    this.InternalPanel = (Panel)this.ItemsPanel.LoadContent();

        //    this.InternalPanel.IsItemsHost = false;

        //    this.InitializeInternalChildren();
        //}

        //private void InitializeInternalChildren()
        //{
        //    foreach (var item in this.Children)

        //        this.InternalPanel.Children.Add(new ProxyUIElement((UIElement)item));
        //}

        //protected override Size MeasureOverride(Size constraint)
        //{
        //    this.InternalPanel.Measure(constraint);

        //    return this.InternalPanel.DesiredSize;
        //}

        //protected override Size ArrangeOverride(Size arrangeSize)
        //{
        //    this.InternalPanel.Arrange(new Rect(arrangeSize));

        //    foreach (ProxyUIElement child in this.InternalPanel.Children)
        //    {
        //        Point point = child.TranslatePoint(new Point(), this.InternalPanel);

        //        child.Element.Arrange(new Rect(point, child.RenderSize));
        //    }

        //    return this.InternalPanel.RenderSize;
        //}

        //protected override void OnVisualChildrenChanged(DependencyObject visualAdded, DependencyObject visualRemoved)
        //{
        //    if (visualAdded != null)

        //        this.InternalPanel.Children.Insert(this.Children.IndexOf((UIElement)visualAdded), new ProxyUIElement((UIElement)visualAdded));

        //    if (visualRemoved != null)

        //        this.InternalPanel.Children.RemoveAt(this.InternalPanel.Children.IndexOf((UIElement)visualRemoved));

        //    base.OnVisualChildrenChanged(visualAdded, visualRemoved);
        //}

        //class ProxyUIElement
        //    : FrameworkElement
        //{
        //    public ProxyUIElement(UIElement element)
        //        : base()
        //    {
        //        this.Element = element;
        //    }
        //    public UIElement Element { get; private set; }

        //    protected override Size MeasureOverride(Size constraint)
        //    {
        //        this.Element.Measure(constraint);

        //        return this.Element.DesiredSize;
        //    }
        //}
    }
}
